package com.wandousoushu.jiusen.booksource.model

import androidx.lifecycle.MutableLiveData
import com.wandousoushu.jiusen.MyApplication
import com.wandousoushu.jiusen.booksource.data.entities.Book
import com.wandousoushu.jiusen.web.http.MainHttpUtils
import java.util.ArrayList

class BookManager {

    private val mBooks: MutableList<Book> = ArrayList()
    var bookList = MutableLiveData<List<Book>>()
    var bookShelfByKey = MutableLiveData<List<Book>>()

    companion object {
        private val instance = BookManager()
        fun GetInstance(): BookManager {
            return instance
        }
    }

    fun AddBooktoShelf(book: Book, type: Int) {
        var isContains = false
        for (mBook in mBooks) {
            if (mBook.name.equals(book.name) && mBook.author.equals(book.author) && mBook.bookUrl.equals(book.bookUrl) && mBook.group == type) {
                isContains = true
                break
            }
        }
        if (!isContains) {
            val mbook1 = Book()
            mbook1.group = 2
            mbook1.durChapterPos = book.durChapterPos
            mbook1.durChapterIndex = book.durChapterIndex
            mbook1.durChapterTitle = book.durChapterTitle
            mbook1.author = book.author
            mbook1.coverUrl = book.coverUrl
            mbook1.intro = book.intro
            mbook1.name = book.name
            mbook1.origin = book.origin
            mbook1.originName = book.originName
            mbook1.bookUrl = book.bookUrl
            mbook1.latestChapterTitle = book.latestChapterTitle
            mbook1.tocUrl = book.tocUrl
            MainHttpUtils.getInstance().SetBook(mbook1, type, MyApplication.instance.token)
            mBooks.add(0,mbook1)
            bookList.postValue(mBooks)
        }
    }

    fun AddBooktoMark(book: Book, type: Int) {
        var isContains = false
        for (mBook in mBooks) {
            if (mBook.name.equals(book.name) && mBook.author.equals(book.author) && mBook.bookUrl.equals(book.bookUrl) && mBook.group == 2) {
                val mbook1 = Book()
                mbook1.group = 1
                mbook1.durChapterPos = mBook.durChapterPos
                mbook1.durChapterIndex = mBook.durChapterIndex
                mbook1.durChapterTitle = mBook.durChapterTitle
                mbook1.author = mBook.author
                mbook1.coverUrl = mBook.coverUrl
                mbook1.intro = mBook.intro
                mbook1.name = mBook.name
                mbook1.origin = mBook.origin
                mbook1.originName = mBook.originName
                mbook1.bookUrl = mBook.bookUrl
                mbook1.latestChapterTitle = mBook.latestChapterTitle
                mbook1.tocUrl = mBook.tocUrl
                mBooks.add(0,mbook1)
                bookList.postValue(mBooks)
                isContains = true
                break
            }
        }
        if (!isContains) {
            val mbook1 = Book()
            mbook1.group = 1
            mbook1.durChapterPos = book.durChapterPos
            mbook1.durChapterIndex = book.durChapterIndex
            mbook1.durChapterTitle = book.durChapterTitle
            mbook1.author = book.author
            mbook1.coverUrl = book.coverUrl
            mbook1.intro = book.intro
            mbook1.name = book.name
            mbook1.origin = book.origin
            mbook1.originName = book.originName
            mbook1.bookUrl = book.bookUrl
            mbook1.latestChapterTitle = book.latestChapterTitle
            mbook1.tocUrl = book.tocUrl
            mBooks.add(0,mbook1)
            bookList.postValue(mBooks)
        }
        MainHttpUtils.getInstance().SetBook(book, type, MyApplication.instance.token)
    }

    fun AddBook(book: Book) {
        if (!mBooks.contains(book)) {
            mBooks.add(book)
            bookList.postValue(mBooks)
        }
    }

    fun UpdateBook(newBook: Book) {
        for (book in mBooks) {
            if (book.author.equals(newBook.author) && book.name.equals(newBook.name) && book.bookUrl.equals(newBook.bookUrl) && book.group == 2) {
                MainHttpUtils.getInstance().AddReadRecord(newBook, 2, MyApplication.instance.token)
                book.durChapterTitle = newBook.durChapterTitle
                book.durChapterIndex = newBook.durChapterIndex
                book.durChapterPos = newBook.durChapterPos
                book.durChapterTime=System.currentTimeMillis()
                mBooks.sortByDescending { book -> book.durChapterTime }
                bookList.postValue(mBooks)
                break
            }
        }
    }

    fun UpdateChangeOriginBook(oldbook: Book,newBook: Book) {
        for (book in mBooks) {
            if (book.author.equals(oldbook.author) && book.name.equals(oldbook.name) && book.bookUrl.equals(oldbook.bookUrl)&&book.group == 2) {
                MainHttpUtils.getInstance().UpdateOrigin(oldbook,newBook, 2, MyApplication.instance.token)
                book.name=newBook.name;
                book.author=newBook.author
                book.bookUrl=newBook.bookUrl
                book.tocUrl=newBook.tocUrl
                book.latestChapterTitle=newBook.latestChapterTitle
                book.coverUrl=newBook.coverUrl
                book.intro=newBook.intro
                book.origin=newBook.origin
                book.originName=newBook.originName
                book.durChapterTitle = newBook.durChapterTitle
                book.durChapterIndex = newBook.durChapterIndex
                book.durChapterPos = newBook.durChapterPos
                book.durChapterTime=System.currentTimeMillis()
                mBooks.sortByDescending { book -> book.durChapterTime }
                bookList.postValue(mBooks)
                break
            }
        }
    }

    fun RemoveBook(book: Book, type: Int) {
        if (mBooks.contains(book)) {
            MainHttpUtils.getInstance().SetBook(book, type, MyApplication.instance.token)
            mBooks.remove(book)
            bookList.postValue(mBooks)
        }
    }

    fun RemoveBookShelfs(removeBookList: List<Book>, type: Int) {
        for (book in removeBookList){
            if (mBooks.contains(book)) {
                var s= MainHttpUtils.getInstance().SetBook(book, type, MyApplication.instance.token)
                mBooks.remove(book)
                bookList.postValue(mBooks)
            }
        }
    }

    fun RemoveBookMark(book: Book?, type: Int) {
        if (book == null) return
        var index=0
        for (mBook in mBooks) {
            if (mBook.name.equals(book.name) && mBook.author.equals(book.author) && mBook.bookUrl.equals(book.bookUrl) && mBook.group == type) {
                MainHttpUtils.getInstance().SetBook(mBook, type, MyApplication.instance.token)
                mBooks.removeAt(index)
                bookList.postValue(mBooks)
                break
            }
            index++
        }
    }

    fun GetBookList(): List<Book> {
        return mBooks
    }

    fun GetBookListByType(type: Int): List<Book> {
        val newBookList: MutableList<Book> = ArrayList()
        for (book in mBooks) {
            if (book.group == type) {
                newBookList.add(book)
            }
        }
        return newBookList
    }

    fun GetBookShelfBykey(key: String?): List<Book> {
        val newBookList: MutableList<Book> = ArrayList()
        for (book in mBooks) {
            if (book.group == 2 && (book.name.contains(key!!) || book.author.contains(key))) {
                newBookList.add(book)
            }
        }
        bookShelfByKey.postValue(newBookList)
        return newBookList
    }

    fun GetBookShlef(name: String, author: String): Book? {
        for (book in mBooks) {
            if (book.name == name && book.author == author && book.group == 2) {
                return book
            }
        }
        return null
    }

    fun GetBookMark(name: String, author: String): Book? {
        for (book in mBooks) {
            if (book.name == name && book.author == author && book.group == 1) {
                return book
            }
        }
        return null
    }

    fun GetBookByUrl(bookUrl: String): Book? {
        for (book in mBooks) {
            if (book.bookUrl == bookUrl) {
                return book
            }
        }
        return null
    }

    fun GetFirstBook(): Book {
        return mBooks[0]
    }

    fun IsInBookShelf(book: Book): Boolean {
        var isContains = false
        for (mBook in mBooks) {
            if (mBook.name.equals(book.name) && mBook.author.equals(book.author) && mBook.bookUrl.equals(book.bookUrl) && mBook.group == 2) {
                isContains = true
                break
            }
        }
        return isContains
    }

    fun clear(){
        mBooks.clear()
        bookList.postValue(mBooks)
        val newBookList: MutableList<Book> = ArrayList()
        bookShelfByKey.postValue(newBookList)
    }
}